package com.xlf.service.blog.controller;

import com.xlf.common.core.annotation.Anonymous;
import com.xlf.common.core.annotation.Log;
import com.xlf.common.core.enums.BusinessType;
import com.xlf.common.core.enums.OperatorType;
import com.xlf.common.core.pojo.mode.AjaxResult;
import com.xlf.common.core.pojo.mode.BaseController;
import com.xlf.common.core.util.page.TableDataInfo;
import com.xlf.service.blog.domain.Article;
import com.xlf.service.blog.domain.dto.AddArticleDTO;
import com.xlf.service.blog.domain.dto.ArticleQueryDTO;
import com.xlf.service.blog.domain.dto.EditArticleDTO;
import com.xlf.service.blog.service.ArticleService;
import com.xlf.service.blog.service.ThumbsService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/blog/article")
public class ArticleController extends BaseController {

    @Autowired
    ArticleService articleService;

    @Autowired
    ThumbsService thumbsService;

    /**
     * 获取文章列表
     * @param articleQueryDto
     * @return
     */
    @Anonymous
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ArticleQueryDTO articleQueryDto){
        startPage();
        return success(getDataTable(articleService.getPage(articleQueryDto)));
    }

    /**
     * 获取最近一页博客列表
     * @return
     */
    @Anonymous
    @GetMapping("/getRecentlyArticle")
    public AjaxResult getRecentlyArticle(){
        return success(getDataTable(articleService.getRecentlyArticle()));
    }


    /**
     * 获取博客信息
     * @return
     */
    @Anonymous
    @GetMapping("/getArticleInfo")
    public AjaxResult getArticleInfo(){
        return success(articleService.getArticleInfo());
    }

    /**
     * 获取文章
     * @param articleId
     * @return
     */
    @Anonymous
    @GetMapping("/{id}")
    public AjaxResult query(@PathVariable(value = "id") @Validated @NotNull(message = "文章不能为空") Long articleId){

        return success(articleService.getArticleById(articleId));
    }

    /**
     * 新增文章
     */
    @Log(mode = "讨论模块",businessType = BusinessType.INSERT,operatorType = OperatorType.MOBILE)
    @PreAuthorize("@ss.hasPermi('blog:article:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody AddArticleDTO article) {
        articleService.saveArticle(article);

        return success();
    }


    /**
     * 修改文章
     */
    @Log(mode = "讨论模块",businessType = BusinessType.UPDATE,operatorType = OperatorType.MOBILE)
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('blog:article:update')")
    public AjaxResult update(@Validated @RequestBody EditArticleDTO article) {
        articleService.updateArticle(article);
        return success();
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermi('blog:article:delete')")
    public AjaxResult delete(@PathVariable Long id){
        return toAjax(articleService.deleteArticle(id));
    }

    /** 点赞
     * @param articleId
     * @return
     */
    @GetMapping("/thumbs/{id}")
    public AjaxResult thumbs(@PathVariable(value = "id") @Validated @NotNull(message = "文章不能为空") Long articleId){
        articleService.thumbs(articleId);
        return success();
    }

    /** 收藏
     * @param articleId
     * @return
     */
    @GetMapping("/collection/{id}")
    public AjaxResult collection(@PathVariable(value = "id") @Validated @NotNull(message = "文章不能为空") Long articleId){
        articleService.collection(articleId);
        return success();
    }

    /** 关注
     * @param userId
     * @return
     */
    @GetMapping("/follow/{id}")
    public AjaxResult follow(@PathVariable(value = "id") @Validated @NotNull(message = "用户不能为空") Long userId){
        articleService.follow(userId);
        return success();
    }

}
